package com.zhny.aiot.louverbox.co2.plugin;

import com.zhny.aiot.louverbox.co2.plugin.model.LouverBoxStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;

public class LouverBoxDevice extends ModbusNumDevice<LouverBoxStoreEntry> {

    public LouverBoxDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
    }

    @Override
    public LouverBoxStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        LouverBoxStoreEntry entry = new LouverBoxStoreEntry ();
        entry.setHumidity (modemResponseMessage.getData ().readUnsignedShort ()/10.0);
        entry.setTemperature (modemResponseMessage.getData ().readUnsignedShort ()/10.0);
        entry.setNoise (modemResponseMessage.getData ().readUnsignedShort () / 10.0);
        entry.setCo2 (modemResponseMessage.getData ().readUnsignedShort ());
        int xx = modemResponseMessage.getData ().readUnsignedShort ();
        entry.setAtmosphericPressure (modemResponseMessage.getData ().readUnsignedShort () / 10.0);
        int illHigh = modemResponseMessage.getData ().readUnsignedShort ();
        int illLow = modemResponseMessage.getData ().readUnsignedShort ();
        entry.setIllumination ((illHigh << 8)|illLow);
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 16;
    }

    @Override
    protected ModemRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
